package kr.hkit.powerjava.chapter14.windowbuilder;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CardLayoutExWin extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel panel;
	private JButton btnFirst;
	private JButton btnprev;
	private JButton btnNext;
	private JButton btnLast;
	private JButton btnExit;
	private JPanel panel_card;
	private CardLayout cardLayout;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CardLayoutExWin frame = new CardLayoutExWin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CardLayoutExWin() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(0, 5, 0, 0));
		
		btnFirst = new JButton("<<");
		btnFirst.addActionListener(this);
		panel.add(btnFirst);
		
		btnprev = new JButton("<");
		btnprev.addActionListener(this);
		panel.add(btnprev);
		
		btnNext = new JButton(">");
		btnNext.addActionListener(this);
		panel.add(btnNext);
		
		btnLast = new JButton(">>");
		btnLast.addActionListener(this);
		panel.add(btnLast);
		
		btnExit = new JButton("종료");
		btnExit.addActionListener(this);
		panel.add(btnExit);
		
		panel_card = new JPanel();
		cardLayout = new CardLayout(0, 0);
		panel_card.setLayout(cardLayout);
		for(int i = 1; i <= 10; i++){
			panel_card.add(new JButton("현재 카드번호는 " + i + "입니다."));
		}
		
		contentPane.add(panel_card, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("종료")){
			System.exit(0);
		}else if(e.getActionCommand().equals("<<")){
			cardLayout.first(panel_card);
		}else if(e.getActionCommand().equals("<")){
			cardLayout.previous(panel_card);
		}else if(e.getActionCommand().equals(">")){
			cardLayout.next(panel_card);
		}else if(e.getActionCommand().equals(">>")){
			cardLayout.last(panel_card);
		}
	}
}
